package com.software.modelcenter.utils;

import com.aspose.threed.ObjSaveOptions;
import com.aspose.threed.Scene;
import com.aspose.threed.StlSaveOptions;
import org.lwjgl.assimp.AIScene;
import org.lwjgl.assimp.Assimp;

import java.io.*;

/**
 * 格式转化工具
 */
public class FormatUtil {

    private static String PATH = "model/";

    /**
     * @param s : 十六进制文件
     * @return s : 转化为字符串
     */
    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    public static void FbxToObj(String file, String id) {
        // 在 Scene 对象中加载 FBX
        com.aspose.threed.TrialException.setSuppressTrialException(true);
        Scene document = null;
        try {
//            document = new Scene(PATH + "123.fbx");
            document = new Scene();
            document.open(PATH + "123.fbx");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 创建 ObjSaveOptions 的实例
        ObjSaveOptions options = new ObjSaveOptions();
        // 将 FBX 保存为 OBJ
        try {
            String fileName = PATH + id + ".obj";
            document.save(fileName, options);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void FbxToStl(String file, String id) {
        // 在 Scene 对象中加载 FBX
        com.aspose.threed.TrialException.setSuppressTrialException(true);
        Scene document = null;
        try {
            document = new Scene(PATH + "123.fbx");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建 ObjSaveOptions 的实例
        StlSaveOptions options = new StlSaveOptions();
        // 将 FBX 保存为 OBJ
        try {
            String fileName = PATH + id + ".stl";
            document.save(fileName, options);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void exportFbx2Obj(String id){
        AIScene Aiscene = Assimp.aiImportFile("model/123.fbx", Assimp.aiProcess_Triangulate);
        AIScene scene = Assimp.aiCopyScene(Aiscene);
        if (scene == null) {
            // 加载文件失败，处理错误
            System.err.println("无法加载FBX文件");
            System.exit(1);
        }
        String outputFile = PATH + id + ".obj";
        int code = Assimp.aiExportScene(scene, "obj", outputFile, Assimp.aiProcess_Triangulate);
        System.out.println(code);
    }


    public static void formatOBJ(String fileName) {
        fileName = PATH + fileName + ".obj";
        StringBuffer buffer = new StringBuffer();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)))) {
            String str = null;
            while ((str = reader.readLine()) != null) {
                str = str.replace("  ", " ");
                buffer.append(str + "\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))){
            writer.write(buffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
